﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Qianke.Core.Common.Decode
{
    public static class BytesEncryptHelper
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string EncodeBytes(byte[] source)
        {
            StringBuilder strBuf = new StringBuilder();
            foreach (byte t in source)
            {
                strBuf.Append((char)(((t >> 4) & 0xF) + ((int)'a')));
                strBuf.Append((char)(((t) & 0xF) + ((int)'a')));
            }
            return strBuf.ToString();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] DecodeBytes(string data)
        {
            byte[] bytes = new byte[data.Length / 2];
            for (int i = 0; i < data.Length; i += 2)
            {
                char c = data[i];
                bytes[i / 2] = (byte)((c - 'a') << 4);
                c = data[i + 1];
                bytes[i / 2] += (byte)(c - 'a');
            }
            return bytes;
        }
    }
}
